/*------------------------------

	Space Is The Place v 0.1
		by Sestze

------------------------------*/

-- Make some Asteroids.

SITPAsteroids = {}
	SITPAsteroids[0] = "models/props_wasteland/rockcliff01j.mdl"
	SITPAsteroids[1] = "models/props_wasteland/rockcliff01b.mdl"
	SITPAsteroids[2] = "models/props_wasteland/rockcliff01c.mdl"
	SITPAsteroids[3] = "models/props_wasteland/rockcliff01e.mdl"
	SITPAsteroids[4] = "models/props_wasteland/rockcliff01f.mdl"
	SITPAsteroids[5] = "models/props_wasteland/rockcliff01g.mdl"
	
SITPAsteroidMats = {}
	SITPAsteroidMats[0] = "models/props_trainstation/trainstation_ornament001"
	SITPAsteroidMats[1] = "models/props_pipes/destroyedpipes01a"
	SITPAsteroidMats[2] = "models/props_wasteland/metal_tram001a"
	SITPAsteroidMats[3] = "models/props_mining/ceiling_junk"
	SITPAsteroidMats[4] = "models/props_explosive/explosive_butane_can_chunk"

SITP_TotalAsteroids = 0					
					
CreateConVar( "sitp_maxasteroids", 30)
CreateConVar( "sitp_asteroidwave", 15)
					
function GM:SITP_CreateAsteroid()
	local maxchecks = 50
	local APos = Vector(0, 0, 0)
	local boolc = false
	for i=1, maxchecks do
		if (APos == Vector(0, 0, 0) or self:SITP_IsInPlanet( APos ) != "space" or !util.IsInWorld( APos )) then
			APos = Vector(math.random(-16000, 16000), math.random(-16000, 16000), math.random(-16000, 16000))
		else
			boolc = true
		end
	end
	if(boolc == true) then
		SITP_TotalAsteroids = SITP_TotalAsteroids + 1
		
		local model = math.random(0, table.Count(SITPAsteroids)-1)
		local hp = 100 + math.random(0, 4) * 100
		local mass = 10 * hp
		local score = hp / 50
		local matnum = math.random(0, table.Count(SITPAsteroidMats)-1)
				
		local ent = ents.Create("sitp_asteroid")
		ent:SetPos(APos)
		ent:SetAngles(Vector(math.random(0, 360), math.random(0, 360), math.random(0, 360)))
		ent:SetNetworkedNumber( "maxhealth", hp )
		ent:SetMaterial(SITPAsteroidMats[matnum])
		
		ent.Model = SITPAsteroids[model]
		ent.Mass = mass
		ent.SITP_AsteroidHealth = hp
		ent.SITP_Score	= score
		
		ent:Spawn()
		
		local phys = ent:GetPhysicsObject()
		if(phys and phys:IsValid()) then
			phys:SetMass(ent.Mass)
			phys:Wake()
			phys:EnableGravity( false )
			phys:EnableDrag ( false )
			phys:SetVelocity(Vector(0, 0, 0))
			--phys:AddAngleVelocity(Vector(math.random(0, 100), math.random(0, 100), math.random(0, 100)))
		end
		
		print("Asteroid Created at: " .. tostring(APos) .. " with a model of " .. tostring(ent.Model) .. " with a model id of " .. tostring(model))
	end
end
					
function GM:SITP_MakeAsteroids( num )
	for i=1, num do
		self:SITP_CreateAsteroid()
	end
end